﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Unity;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows;

namespace Infrastructure.WpfClient.Composite.Regions.LateralRegion
{
    public class LateralRegionAdapter : RegionAdapterBase<LateralRegionControl>
    {
        [Dependency]
        public IUnityContainer Container { get; set; }
            public LateralRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
              : base(regionBehaviorFactory)
        {
        
        }
        
        protected override Microsoft.Practices.Composite.Regions.IRegion CreateRegion()
        {
            return Container.Resolve<LateralViewRegion>();
        }
        protected override void Adapt(Microsoft.Practices.Composite.Regions.IRegion region, LateralRegionControl regionTarget)
        {
            var stack = new StackPanel() { Orientation = Orientation.Vertical }; ;
            regionTarget.Content = stack;
            region.ActiveViews.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        var view =  e.NewItems[0] as UserControl;                        
                        if (view != null)
                        {
                            
                            var expand = new Expander();
                            expand.Style = (Style)Application.Current.Resources["LateralExpanderWorkspaceStyle"];
                            expand.ExpandDirection = ExpandDirection.Down;
                            expand.IsExpanded = true;                            
                            expand.Content = view;
                            stack.Children.Add(expand);
                        }                            
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        var oview = e.OldItems[0];
                        Expander oviewexp = null;
                        foreach (Expander item in stack.Children)
                        {
                            if (item.Content == oview)
                            {
                                oviewexp = item;
                            }                                                        
                        }
                        stack.Children.Remove(oviewexp);
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                        stack.Children.Clear();

                        break;
                    default:
                        break;
                }
                
            };            
            region.Views.CollectionChanged += (sender , e)=>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        region.Activate(e.NewItems[0]);
                    break;                                        
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        region.Deactivate(e.OldItems[0]);
                    break;
                }
            };
        }
    }
}
